<template>
  <div>
    <div v-for="(i,index) in list">
      <van-field v-for="j in i" :key="j.id" :label="j.lable" v-model="values[index+j.key]"></van-field>
      <p>---------------------------------------------</p>
    </div>
    <van-button @click="add" style="background-color: cornflowerblue">+</van-button>
    <van-button @click="subit" round style="background-color: aqua">添加</van-button>

  </div>
</template>

<script>
export default {
  name: "add_parms",
  data() {
    return {
      list: [[{'lable': '名称', 'key': 'name'}, {'lable': '类型', 'key': 'type'}, {
        'lable': '字段名',
        'key': 'fname'
      }, {'lable': '值', 'key': 'fvalue'},]],
      id: this.$route.query.id,
      numbers: 1,
      values: {},

    }
  },
  methods: {
    add() {
      this.numbers += 1
      this.list.push([{'lable': '名称', 'key': 'name'}, {'lable': '类型', 'key': 'type'}, {
        'lable': '字段名',
        'key': 'fname'
      }, {'lable': '值', 'key': 'fvalue'},])
    },
    subit() {
      var list = []
      for (var i = 0; i < this.numbers; i++) {
        var name = i + 'name'
        var type = i + 'type'
        var fname = i + 'fname'
        var fvalue = i + 'fvalue'
        var dict = {
          'name': this.values[name],
          'type': this.values[type],
          'fname': this.values[fname],
          'fvalue': this.values[fvalue],
        }
        list.push(dict)
      }
      console.log(list)
      this.$axios.post('/day01/update', {'id': this.id, 'parms': JSON.stringify(list)})
        .then(res => {
          if (res.data.code == 200) {
            alert('添加成功')
          }
        })
    },
    getparms() {
      this.$axios.get('/day01/update?id=' + this.id)
        .then(res => {
          if (res.data.code == 200) {
            this.parms = res.data.list
          }
        })
    }
  },
  mounted() {
    this.getparms()
  }
}
</script>

<style scoped>

</style>
